package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.mapper.BorrowerAttachMapper;
import com.atguigu.srb.core.pojo.entity.BorrowerAttach;
import com.atguigu.srb.core.pojo.vo.BorrowerAttachVO;
import com.atguigu.srb.core.service.BorrowerAttachService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 借款人上传资源表 服务实现类
 * </p>
 *
 * @author javaCode
 * @since 2025-07-19
 */
@Service
public class BorrowerAttachServiceImpl extends ServiceImpl<BorrowerAttachMapper, BorrowerAttach> implements BorrowerAttachService {

    /**
     * 获取附件
     */
    @Override
    public List<BorrowerAttachVO> selectBorrowerAttachVOList(Long id) {
        // 防御性编程：处理id为空的情况
        if (id == null) {
            return Collections.emptyList();
        }

        // 查询借款人附件列表
        LambdaQueryWrapper<BorrowerAttach> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BorrowerAttach::getBorrowerId, id);
        List<BorrowerAttach> borrowerAttachList = this.baseMapper.selectList(queryWrapper);

        // 使用Stream API简化转换过程
        return borrowerAttachList.stream().map(attach -> {
            BorrowerAttachVO vo = new BorrowerAttachVO();
            vo.setImageType(attach.getImageType());
            vo.setImageUrl(attach.getImageUrl());
            return vo;
        }).collect(Collectors.toList());
    }
}
